<template>
  <div>
    <a-card :bordered="false" >
      <!-- 增加 -->
      <sys-auto-code-part-add-form
        v-if="showAddModal"
        ref="sysAutoCodePartAddForm"
        :partTypeOptions="partTypeOptions"
        :cycleFlagOptions="cycleFlagOptions"
        :cycleMethodOptions="cycleMethodOptions"
        :ruleId="ruleId"
        @ok="getList"
        @close="showAddModal = false"
      />
      <!-- 编辑 -->
      <sys-auto-code-part-edit-form
        v-if="showEditModal"
        ref="sysAutoCodePartEditForm"
        :partTypeOptions="partTypeOptions"
        :cycleFlagOptions="cycleFlagOptions"
        :cycleMethodOptions="cycleMethodOptions"
        :ruleId="ruleId"
        @ok="getList"
        @close="showEditModal = false"
      />
      <div class="table-operations">
        <a-button type="primary" @click="handleAdd" v-hasPermi="['system:sysAutoCodePart:add']">
          <a-icon type="plus" />新增
        </a-button>
		<a-button type="danger" v-if="!multiple" :disabled="multiple" @click="handleDelete" v-hasPermi="['system:sysAutoCodePart:remove']">
          <a-icon type="delete" />删除
        </a-button>
        <a-button type="" @click="handleExport" v-hasPermi="['system:sysAutoCodePart:export']">
          <a-icon type="download" />导出
        </a-button>
        <a-tooltip title="刷新">
          <a-icon @click="getList" class="action" :type="loading ? 'loading' : 'reload'" />
        </a-tooltip>
      </div>
      <a-table
        :loading="loading"
        rowKey="id"
        size="middle"
        @change="handleTableChange"
        @refresh="getList"
        :columns="columns"
        :data-source="sysAutoCodePartList"
        :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        :pagination="false"
      >
        <span slot="partType" slot-scope="record">
          {{ partTypeFormat(record) }}
        </span>
        <span slot="cycleFlag" slot-scope="record">
          {{ cycleFlagFormat(record) }}
        </span>
        <span slot="cycleMethod" slot-scope="record">
          {{ cycleMethodFormat(record) }}
        </span>
        <span slot="operation" slot-scope="text, record">
          <a @click="handleUpdate(record)" v-hasPermi="['system:sysAutoCodePart:edit']">
            修改
          </a>
          <a-divider type="vertical" v-hasPermi="['system:sysAutoCodePart:remove']"/>
          <a @click="handleDelete(record)" v-hasPermi="['system:sysAutoCodePart:remove']">
            删除
          </a>
        </span>
      </a-table>
    </a-card>
  </div>
</template>
<script>
import { listSysAutoCodePart, delSysAutoCodePart, exportSysAutoCodePart, getInitData } from '@/api/system/sysAutoCodePart'
import AdvanceTable from '@/components/pt/table/AdvanceTable'
import SysAutoCodePartAddForm from '@/views/system/sysautocoderule/modules/SysAutoCodePartAddForm'
import SysAutoCodePartEditForm from '@/views/system/sysautocoderule/modules/SysAutoCodePartEditForm'
export default {
  name: 'SysAutoCodePart',
    props: {
      ruleId: {
        type: String,
        required: true
      },
      title: {
        type: String,
        default: '子表'
      }
    },
  components: {
    AdvanceTable,
    SysAutoCodePartAddForm,
    SysAutoCodePartEditForm

  },
  data () {
    return {
      showAddModal: false,
      showEditModal: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中的主键集合
      selectedRowKeys: [],
      // 选中的数据集合
      selectedRows: [],
      // 高级搜索 展开/关闭
      advanced: false,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // label的百分比
      labelCol: { span: 6 },
      // 内容区域的百分比
      wrapperCol: { span: 18 },
      // 编码生成规则组成表表格数据
      sysAutoCodePartList: [],
      // 分段类型字典
      partTypeOptions: [],
      // 流水号是否循环字典
      cycleFlagOptions: [],
      // 循环方式，YEAR：按年，MONTH：按月，DAY：按天，HOUR：按小时，MINITE：按分钟，OTHER：按传入字符变字典
      cycleMethodOptions: [],
      // 查询参数
      queryParam: {
        pageNum: 1,
        pageSize: 1000
      },
      columns: [
        {
          title: '分段序号',
          dataIndex: 'partIndex',
          align: 'right',
          width: '15%'
        },
        {
          title: '分段类型',
          dataIndex: 'partType',
          scopedSlots: { customRender: 'partType' },
          align: 'center',
          width: '15%'
        },
        {
          title: '分段编号',
          dataIndex: 'partCode',
          ellipsis: true,
          align: 'left',
          width: '15%'
        },
        {
          title: '分段名称',
          dataIndex: 'partName',
          ellipsis: true,
          align: 'left',
          width: '15%'
        },
        {
          title: '分段长度',
          dataIndex: 'partLength',
          align: 'right',
          width: '15%'
        },
        {
          title: '日期时间格式',
          dataIndex: 'dateFormat',
          ellipsis: true,
          align: 'left',
          width: '15%'
        },
        {
          title: '操作',
          dataIndex: 'operation',
          align: 'center',
          width: '10%',
          scopedSlots: { customRender: 'operation' }
        }
      ]
    }
  },
  created () {
    this.getList()
    getInitData('serial_number_part_type,sys_yes_no,serial_number_circulate_type').then(response => {
      this.partTypeOptions = response.data.serial_number_part_type
      this.cycleFlagOptions = response.data.sys_yes_no
      this.cycleMethodOptions = response.data.serial_number_circulate_type
    })
  },
  methods: {
    /** 查询编码生成规则组成表列表 */
    getList () {
      this.loading = true
      this.queryParam.ruleId = this.ruleId
      listSysAutoCodePart(this.queryParam).then(response => {
        this.sysAutoCodePartList = response.data.list
        this.total = response.data.total
        this.loading = false
      })
    },
    // 分段类型字典翻译
    partTypeFormat (row) {
      if (row.partType) {
        return this.selectDictLabel(this.partTypeOptions, row.partType)
      } else {
        return ''
      }
    },
    // 流水号是否循环字典翻译
    cycleFlagFormat (row) {
      if (row.cycleFlag) {
        return this.selectDictLabel(this.cycleFlagOptions, row.cycleFlag)
      } else {
        return ''
      }
    },
    // 循环方式，YEAR：按年，MONTH：按月，DAY：按天，HOUR：按小时，MINITE：按分钟，OTHER：按传入字符变字典翻译
    cycleMethodFormat (row) {
      if (row.cycleMethod) {
        return this.selectDictLabel(this.cycleMethodOptions, row.cycleMethod)
      } else {
        return ''
      }
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParam.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.queryParam = {
        pageNum: 1,
        pageSize: 1000
      }
      this.handleQuery()
    },
    /** 翻页操作 */
    onShowSizeChange (current, pageSize) {
      this.queryParam.pageSize = pageSize
      this.getList()
    },
    /** 翻页操作 */
    onSizeChange (current, size) {
      this.queryParam.pageNum = 1
      this.queryParam.pageSize = size
      this.getList()
    },
    /** 翻页操作 */
    changeSize (current, pageSize) {
      this.queryParam.pageNum = current
      this.queryParam.pageSize = pageSize
      this.getList()
    },
    /** 翻页操作 */
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
      this.ids = this.selectedRows.map(item => item.id)
      this.single = selectedRowKeys.length !== 1
      this.multiple = !selectedRowKeys.length
    },
    /** 查询折叠和展开操作 */
    toggleAdvanced () {
      this.advanced = !this.advanced
    },
    handleAdd () {
      this.showAddModal = true
      this.$nextTick(() => (
        this.$refs.sysAutoCodePartAddForm.handleAdd()
      ))
    },
    handleUpdate (record, ids) {
      this.showEditModal = true
      this.$nextTick(() => (
        this.$refs.sysAutoCodePartEditForm.handleUpdate(record, ids)
      ))
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      var that = this
      const sysAutoCodePartIds = row.id || this.ids
      this.$confirm({
        title: '确认删除所选中数据?',
        onOk () {
          return delSysAutoCodePart(sysAutoCodePartIds)
            .then(() => {
              that.onSelectChange([], [])
              that.getList()
              that.$message.success(
                '删除成功',
                3
              )
          })
        },
        onCancel () {}
      })
    },
    /** 导出按钮操作 */
    handleExport () {
      var that = this
      this.$confirm({
        title: '是否确认导出?',
        content: '此操作将导出当前条件下所有数据而非选中数据',
        onOk () {
          return exportSysAutoCodePart(that.queryParam)
            .then(response => {
              that.download(response.msg)
              that.$message.success(
                '导出成功',
                3
              )
          })
        },
        onCancel () {}
      })
    },
    handleTableChange (pagination, filters, sorter) {
      if (sorter.field !== undefined && sorter.field !== null && sorter.field !== '') {
        this.queryParam.orderByColumn = 'a.' + sorter.field
        this.queryParam.isAsc = sorter.order
      }
      this.getList()
    }
  }
}
</script>
